package com.zhss.demo.register.server;

/**
 * 服务实力
 * 里面包含了服务实例的全部信息
 * 比如：服务名称、ip、主机名、端口号、服务实例ID
 * 还有契约信息Lease
 * @author gubatian
 * @date 2021/9/7 5:21 下午
 */
public class ServiceInstance {
    private final static long NOT_ALIVE_PERIOD = 90 * 1000L;
    /**
     * 服务名称
     */
    private String serviceName;
    /**
     * ip地址
     */
    private String ip;
    /**
     * 主机名
     */
    private String hostname;
    /**
     * 端口号
     */
    private int port;
    /**
     * 服务实例id
     */
    private String serviceInstanceId;
    /**
     * 契约
     */
    private Lease lease = new Lease();

    public void renew() {
        lease.renew();
    }
    public Boolean isAlive() {
        return lease.isAlive();
    }
    /**
     * 契约
     * 维护了服务实例和当前服务注册中心的联系
     * 包括心跳时间、创建时间等等
     */
    private class Lease {
        /**
         * 最后一次心跳时间
         */
        private long latestHeartbeatTime = System.currentTimeMillis();

        /**
         * 契约
         * 你只要发送了一次心跳，就相当于把register-client与register-server之间维护的一个契约进行了续约
         * register-client告诉register-server我还存活着，我们两的契约可以维持着
         */
        public void renew() {
            latestHeartbeatTime = System.currentTimeMillis();
            System.out.println("服务实例【" + serviceInstanceId + "】，进行续约：" + latestHeartbeatTime);
        }

        /**
         * 判断当前服务实例契约是否存活
         * @return
         */
        public Boolean isAlive() {
            Long currentTime = System.currentTimeMillis();
            if(currentTime - latestHeartbeatTime > NOT_ALIVE_PERIOD) {
                System.out.println("服务实例【" + serviceInstanceId + "】，不再存活");
                return false;
            }
            System.out.println("服务实例【" + serviceInstanceId + "】，保持存活");
            return true;
        }
    }

    public String getServiceName() {
        return serviceName;
    }

    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getHostname() {
        return hostname;
    }

    public void setHostname(String hostname) {
        this.hostname = hostname;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getServiceInstanceId() {
        return serviceInstanceId;
    }

    public void setServiceInstanceId(String serviceInstanceId) {
        this.serviceInstanceId = serviceInstanceId;
    }

    public Lease getLease() {
        return lease;
    }

    public void setLease(Lease lease) {
        this.lease = lease;
    }

    @Override
    public String toString() {
        return "ServiceInstance{" +
                "serviceName='" + serviceName + '\'' +
                ", ip='" + ip + '\'' +
                ", hostname='" + hostname + '\'' +
                ", port=" + port +
                ", serviceInstanceId='" + serviceInstanceId + '\'' +
                ", lease=" + lease +
                '}';
    }
}
